@layout("/common/default.html",{title:'添加',libs:['form']}){
    <!--form表单-->
    <form class="layui-form"  lay-filter="layui-form-user" id="layui-form-user">
        <input type="hidden" id="onlyid" name="id" value="${user.id}">
        <input type="hidden" id="op"  value="${user.op}">
        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-card-header">基本信息</div>
                <div class="layui-card-body">
                    <div class="layui-form-item layui-row">
                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label layui-required">用户名</label>
                            <div class="layui-input-block">
                                <input type="text" id="name" name="name" value="${user.name!}"  lay-verify="required" placeholder="请输入"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label layui-required">登录名</label>
                            <div class="layui-input-block">
                                <input type="text"  id="loginName" name="loginName" value="${user.loginName!}" lay-verify="required" placeholder="请输入"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label">手机号码</label>
                            <div class="layui-input-block">
                                <input type="text" id="mobile" name="mobile" value="${user.mobile!}"  lay-verify="phone" maxlength="11" placeholder="请输入"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label">电话</label>
                            <div class="layui-input-block">
                                <input type="text"  id="phone" name="phone" value="${user.phone!}" lay-verify="number" placeholder="请输入"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-block">
                                <input type="text"  id="email" name="email" value="${user.email!}" lay-verify="email" placeholder="请输入"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label layui-required">所属部门</label>
                            <div class="layui-input-block">
                                <input type="text" id="office" name="office" placeholder="请输入" autocomplete="off" class="layui-input" lay-verify="required" >
                            </div>
                        </div>

                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label layui-required">用户类型</label>
                            <div class="layui-input-block">
                                <#select path="userType" value="${user.userType!}" items="${fnc.getDictList('sys_user_type')}" layVerify="layVerify" placeholder="请选择"/>
                            </div>
                        </div>

                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label">职务</label>
                            <div class="layui-input-block">
                                <input type="text"  id="job" name="job"  value="${user.job!}" lay-verify="" placeholder="请输入"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline layui-col-md9" >
                            <label class="layui-form-label" >是否允许登陆</label>
                            <div class="layui-input-block">
                                <input type="radio" name="loginFlag" value="0" title="启用" lay-filter="userFlag" checked><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><div>是</div></div>
                                <input type="radio" name="loginFlag" value="1" title="停用" lay-filter="userFlag"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><div>否</div></div>
                            </div>
                        </div>


                       <!-- <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label">排序</label>
                            <div class="layui-input-block">
                                <input type="text" id="sort" name="sort"  value="${user.sort!}" lay-verify="required" placeholder="请输入"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>-->

                        <div class="layui-inline layui-col-md9">
                            <label class="layui-form-label layui-required">选择角色</label>
                            <div class="layui-input-block">
                                @for(item in allRoles){
                                <input type="radio" name="roles" value="${item.id!}" title="${item.name}">
                                @}
                            </div>
                        </div>
                </div>
            </div>

        </div>
            <div class="form-group-bottom" style="z-index: 200" align="center">
            <button class="layui-btn" lay-filter="btnSubmit" lay-submit>&emsp;提交&emsp;</button>
            <button   id="btnclose" class="layui-btn layui-btn-primary"  >&emsp;取消&emsp;</button>
        </div>
        </div>
    </form>
<!--js逻辑-->
<script>

    var groupCheckbox = $("input[name='roles']");
    @if(isNotEmpty(user.roleList)){
    @for(role in user.roleList){
            var value = '${role.id}';
            for (i = 0; i < groupCheckbox.length; i++) {
                if (groupCheckbox[i].value == value) {
                    groupCheckbox[i].checked=true;
                }
            }
        @}
    @}

    layui.use(['form', 'utils', 'submitform','openLayer','treeSelect','resAjax'], function () {
        var $ = layui.$,
            form = layui.form,
            treeSelect = layui.treeSelect,
            openLayer = layui.openLayer,
            resAjax = layui.resAjax,
            submitform =layui.submitform;

        //检测登录名是否存在
        $("#loginName").change(function(obj){
            var json={
                loginName:$("#loginName").val(),
            }
            var obj=resAjax.render({
                url:"/sys/user/checkLoginName", //url
                type:"post",//类型
                data:json,//id
            });
            if(obj.code=="0"){
                layer.msg(obj.msg, {time: 2000, icon: 5});
                $("#loginName").val("");
            }
        });


            treeSelect.render({
                elem: '#office',
                title: '部门选择',
                type: '1',
                icon:'layui-icon layui-icon-share',
                value:'${user.office.id!}',
                label:'${user.office.name!}'
            });



        form.on("submit(btnSubmit)", function (data) {
            var field=data.field;
            var role=$('input:radio[name="roles"]:checked').val();
            if(role==undefined){
                openLayer.yellowSighMsg("角色不能为空");
                return false;
            }else {
                $('.layui-input-block').find('input[name="roles"]:checked').each(function (i) {
                    field['roleList[' + i + ']'] = this.value
                });
                submitform.form('/sys/user/save',field);
                return false;


            }

        });



    });

</script>
@}
